package com.shengyijie.activity.tab;

import java.util.ArrayList;

import org.apache.http.HttpResponse;

import com.shengyijie.activity.R;    
import com.shengyijie.activity.commen.ItemActivity;  
import com.shengyijie.activity.commen.ItemListActivity;  
import com.shengyijie.adapter.view.AlertBox;
import com.shengyijie.adapter.view.AlertBox.OnCanceledListener;
import com.shengyijie.adapter.viewadapter.ItemListAdapter;
import com.shengyijie.context.ContextApplication;
import com.shengyijie.model.http.HttpConnectApi;
import com.shengyijie.model.json.JsonParser;
import com.shengyijie.model.object.Listobject.ProjectList;
import com.shengyijie.model.object.baseobject.Item;

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnKeyListener;
import android.os.AsyncTask;
import android.os.Bundle; 
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class RecommendedActivity extends Activity implements OnClickListener{
 
	private ListView itemListView;
	private ItemListAdapter listAdapter;
	private TextView tv_no;
	private View preView=null; 
	public Task task;  
	private HttpConnectApi httpConnect;
	 private AlertBox mypDialog;
	
	public ArrayList<Item> itemList=new ArrayList<Item>();  
	
	 public void onCreate(Bundle savedInstanceState) {
	      super.onCreate(savedInstanceState); 
	      setContentView(R.layout.recommended); 
	      initView();
	      getRecommededList();
	 } 
	 
	 public void initView(){ 
	    	itemListView=(ListView) this.findViewById(R.id.recommendedlist); 
	    	itemListView.setOnItemClickListener(itemClickListener);
	    	tv_no=(TextView) this.findViewById(R.id.no);
	    	tv_no.setVisibility(View.INVISIBLE); 
	    	 
	 }
	 
	 
	 
	  public void getRecommededList(){
		  itemListView.postDelayed(new Runnable() {  
	             public void run() {  
	            	 mypDialog=new AlertBox(RecommendedActivity.this,RecommendedActivity.this,findViewById(R.id.reclist)); 
	            	 mypDialog.setCompletedListener(new OnCanceledListener(){ 
	 					@Override
	 					public void onCanceled() { 
	 						if(task.getStatus()==AsyncTask.Status.RUNNING){
	 				    		 task.cancel(true); 
	 						  }
	 					}});
	            	 task=new Task();  
	        	     task.execute(TASK_LOAD_ITEM_LIST); 
	             }  
	         },1);
	  }
	  
	  public void getRecommededListAdapter(){  
			 Item content=null;
			 for(int i=0;i<projectList.getCount();i++){
				content= new Item(); 
				content.setID(projectList.getItem(i).getID()); 
				content.setImageUrl(projectList.getItem(i).getPro_pic_show());  
				content.setName(projectList.getItem(i).getPro_name()); 
				content.setDes(projectList.getItem(i).getPro_define());
				itemList.add(content);
			 }  
			 listAdapter = new ItemListAdapter(this, itemList, itemListView);   
	  }
	  
	  ProjectList projectList;
	  public String doGetRecommendedList(){
	     	String result="re_fail";
	        httpConnect=HttpConnectApi.getInstance();
	        try { 
	            HttpResponse response=httpConnect.getRecommendedList("0,50");
	            if (response.getStatusLine().getStatusCode() == 200)
				{ 
	           	    JsonParser jsonParser=JsonParser.getInstance();
	           	    projectList=jsonParser.getAttentionProgect(response);
	           	    if(projectList!=null){
	           	    	if(projectList.getCount()>0){
	           	    		getRecommededListAdapter();
	           	    	    result="re_ok";
	           	    	}
	           	    	   
	           	    	else if(projectList.getCount()==0)
	           	    		result="re_0";
	           	    } 
				}
	            return result;
	        }catch (Exception e) {} 
			return result;
	   }
	  
	  
	  public String doGetProjectDetail(){
	     	String result="pr_fail";
	        httpConnect=HttpConnectApi.getInstance();
	        try { 
	            HttpResponse response=httpConnect.getProjectDetail(pro_id);
	            if (response.getStatusLine().getStatusCode() == 200)
				{ 
	           	    JsonParser jsonParser=JsonParser.getInstance();
	           	    ContextApplication.project=jsonParser.getProgectDetail(response);
	           	    if(ContextApplication.project!=null){ 
	           	    	 result="pr_ok"; 
	           	    } 
				}
	            return result;
	        }catch (Exception e) {} 
			return result;
	 }
	 
	 
	 int pro_id; 
	 public OnItemClickListener itemClickListener = new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try{
					 preView=view;  
		    		 Item content=(Item)parent.getItemAtPosition(position);
		    		 ContextApplication.itemName=content.getName();
		    		 pro_id=content.getID();
			    	 task=new Task();  
	   		         task.execute(TASK_LOAD_ITEM); 
					 
				}catch(Exception e){}
			}
	    };
 
	 
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	 public void changeToItemActivity(){
		 Intent intent=new Intent();
		 intent.setClass(this, ItemActivity.class);
		 this.startActivityForResult(intent,1);
	 }
	 
 
	
     private final static int ALERT_LOADING=1; 
	 private final static int RESULT_LOAD_ITEMDETAIL_OK=16;
	 private final static int RESULT_LOAD_ITEMDETAIL_FAIL=17; 
	 
	 private final static int RESULT_LOAD_ITEMLIST_OK=2;
	 private final static int RESULT_LOAD_ITEMLIST_0=3; 
	 private final static int RESULT_LOAD_ITEMLIST_FAIL=4;
	  
	 private final static String TASK_LOAD_ITEM="loaditem";   
	 private final static String TASK_LOAD_ITEM_LIST="loaditemlist";  
	 private final Handler handler = new Handler(Looper.getMainLooper()) {
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
			 	switch (msg.what) { 
				   case ALERT_LOADING: 
					    mypDialog.show();
					    break; 
				   case RESULT_LOAD_ITEMLIST_FAIL:
					   try{
					      mypDialog.dismiss();
					      Toast.makeText(RecommendedActivity.this, "对不起，暂无数据", Toast.LENGTH_SHORT).show();			               
					      itemListView.setAdapter(null);
			              tv_no.setVisibility(View.VISIBLE);
					    }catch(Exception e){}
					     break;
				    case RESULT_LOAD_ITEMLIST_0:
					     mypDialog.dismiss(); 
					     Toast.makeText(RecommendedActivity.this, "对不起，暂无数据", Toast.LENGTH_SHORT).show();			               
					     break; 
                    case RESULT_LOAD_ITEMLIST_OK:
                    	try{
	                    	 mypDialog.dismiss(); 
	                    	 itemListView.setAdapter(listAdapter); 
                    	 }catch(Exception e){}
					     break; 
                    case RESULT_LOAD_ITEMDETAIL_OK:
                  	     mypDialog.dismiss(); 
                  	     changeToItemActivity();
					     break; 
                   case RESULT_LOAD_ITEMDETAIL_FAIL:
					     mypDialog.dismiss(); 
					     Toast.makeText(RecommendedActivity.this, "对不起，暂无数据", Toast.LENGTH_SHORT).show();			               
					     break; 
				}
			}
		};
		
		
	private class Task extends AsyncTask<String, String, String> {
 
		@Override
		protected String doInBackground(String... params) {
			if(params[0].equals(TASK_LOAD_ITEM)){
				return doGetProjectDetail();
			}else if(params[0].equals(TASK_LOAD_ITEM_LIST)){
				return doGetRecommendedList();
			}else
				return ""; 
		} 
		@Override
		protected void onPostExecute(String result) {				
			super.onPostExecute(result);

			if(result.equals("pr_fail")){
				Message msg = Message.obtain();
				msg.what = RESULT_LOAD_ITEMDETAIL_FAIL;
				handler.sendMessage(msg);
			}else if(result.equals("pr_ok")){
				Message msg = Message.obtain();
				msg.what = RESULT_LOAD_ITEMDETAIL_OK;
				handler.sendMessage(msg);
			} else if(result.equals("re_fail")){
				Message msg = Message.obtain();
				msg.what = RESULT_LOAD_ITEMLIST_FAIL;
				handler.sendMessage(msg);
			}else if(result.equals("re_ok")){
				Message msg = Message.obtain();
				msg.what = RESULT_LOAD_ITEMLIST_OK;
				handler.sendMessage(msg);
			}else if(result.equals("re_0")){
				Message msg = Message.obtain();
				msg.what = RESULT_LOAD_ITEMLIST_0;
				handler.sendMessage(msg);
			}  
		} 
		@Override
		protected void onPreExecute() { 
			super.onPreExecute(); 
		    Message msg = Message.obtain();
		    msg.what = ALERT_LOADING;
			handler.sendMessage(msg); 
		} 
		@Override
		protected void onProgressUpdate(String... values) {
			super.onProgressUpdate(values);
		}
	}
	 
	
	public boolean onKeyDown(int keyCode,KeyEvent event) {   
	    if (keyCode == KeyEvent.KEYCODE_BACK) { 
	    	
				 new AlertDialog.Builder(this).setTitle("提示")
				 .setMessage("确定退出？")
				 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) { 
						android.os.Process.killProcess(android.os.Process.myPid());
					}
				  })
				  .setNegativeButton("取消", new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog, int whichButton) {
					     }
				  })
				  .show();
	    	
	         return true;   
	     } else
	         return super.onKeyDown(keyCode,event);   
	 }
    
	 
}